package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.util.HttpUtil;
import com.smartrobot.smartrobot.view.ToastCustom;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/1/20.
 */
public class ResetPersonMessageActivity extends Activity implements View.OnClickListener{

    private TextView title,detailTxt;
    private ProgressBar progressBar;
    private EditText input;
    private ImageView back;
    private RelativeLayout save;
    private String oldString = "";
    private String key = "";
    private Map<String,String> map;

    private boolean isSetting;
    private ResetHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            throw new NullPointerException();
        }
        this.setContentView(R.layout.page_personal_message_reset);

        init();

    }


    void init(){

        HttpUtil.init(getApplicationContext());
        handler = new ResetHandler();
        initView();

    }

    void initView(){

        isSetting = false;

        back = (ImageView)findViewById(R.id.reset_back);
        title = (TextView)findViewById(R.id.reset_title);
        detailTxt = (TextView)findViewById(R.id.reset_txt);
        input = (EditText)findViewById(R.id.reset_input);
        save = (RelativeLayout)findViewById(R.id.reset_save);
        progressBar = (ProgressBar)findViewById(R.id.reset_progress);

        back.setOnClickListener(this);
        save.setOnClickListener(this);


        //初始化
        Intent intent = this.getIntent();
        title.setText("修改"+ intent.getStringExtra("key"));
        key = intent.getStringExtra("type");

        if(key.equals("7")){//年龄只能输入数字

            Log.e("=============>年龄","");
            input.setVisibility(View.GONE);
            input = (EditText)findViewById(R.id.reset_input_number);
            input.setVisibility(View.VISIBLE);

        }

        if( intent.getStringExtra("value") != null  && !intent.getStringExtra("value").equals("未填写"))
        {
            input.setText(intent.getStringExtra("value"));
            oldString = intent.getStringExtra("value");
        }



    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.reset_back:

                ResetPersonMessageActivity.this.finish();

                break;

            case R.id.reset_save:

                String newString = input.getText().toString();
                 if(newString != null && !newString.equals(oldString) && newString.length() <= 20 && newString.length() > 0){

                     map = new HashMap<String, String>();
                     map.put("key",key);
                     map.put("value",newString);
                     map.put("file","");
                     if(!isSetting){
                         HttpUtil.apiResetPersonMessage(map,handler);
                         progressBar.setVisibility(View.VISIBLE);
                     }


                 }else{
                     ToastCustom.showToast(getApplicationContext(),"输入内容长度有误!");
                 }


                break;
        }

    }

    class ResetHandler extends Handler {

        @Override
        public void dispatchMessage(Message msg) {

            switch (msg.what){

                case HttpUtil.RESET_PERSONAL_MESSAGE_SUCCESS:


                    Intent intent = new Intent();
                    intent.putExtra("what",msg.what);
                    intent.putExtra("arg1",msg.arg1);
                    intent.putExtra("obj",msg.obj.toString());
                    setResult(RESULT_OK, intent);
                    ResetPersonMessageActivity.this.finish();
                    isSetting = false;
                    break;

                case HttpUtil.RESET_PERSONAL_MESSAGE_FAILD:

                    if(msg.obj != null){

                        if(msg.obj.toString().equals("100")){//未登录，应该重新登录

                            HttpUtil.reLogin(getApplication(),handler);

                        }else{
                            ToastCustom.showToast(getApplicationContext(),"请重新提交一次！");
                            progressBar.setVisibility(View.GONE);
                            isSetting = false;
                        }

                    }

                    break;

                case HttpUtil.LOGIN_SUCCESS:

                    HttpUtil.apiResetPersonMessage(map,handler);


                    break;

                case HttpUtil.LOGIN_FAILD:

                    ToastCustom.showToast(getApplicationContext(),"无法连接服务器，请稍后再试！");
                    progressBar.setVisibility(View.GONE);
                    isSetting = false;
                    break;

            }

        }
    }

}
